home *** CD-ROM | disk | FTP | other *** search
-
-
-
- BSEARCH(3) MINTLIB LIBRARY FUNCTIONS BSEARCH(3)
-
-
- N✓NA✓AM✓ME✓E
- bsearch - binary search a sorted table
-
- S✓SY✓YN✓NO✓OP✓PS✓SI✓IS✓S
- #include <stdlib.h>
-
- void *bsearch(const void *key, const void *base,
- size_t total_elems, size_t elem_size,
- int (*compare)(const void *one, const void *two));
-
- D✓DE✓ES✓SC✓CR✓RI✓IP✓PT✓TI✓IO✓ON✓N
- bsearch is a binary search routine generalized from Knuth
- (6.2.1) Algorithm B. It returns a pointer into a table
- indicating where a datum may be found. The table must be
- previously sorted in increasing order according to a pro-
- vided comparison function.
- - key points to a datum instance to be sought in
- the table.
- - base points to the element at the base of the table.
- - total_elems is the number of elements in the table.
- - elem_size is the size, in bytes, of each element
- in the table.
- - compare is the name of the comparison function,
- which is called with two arguments that point to
- the elements being compared. As the function must
- return an integer less than, equal to, or greater
- than zero, so must the first argument to be considered
- be less than, equal to, or greater than the second.
-
- E✓EX✓XA✓AM✓MP✓PL✓LE✓E
- The example below searches a table containing pointers to
- nodes consisting of a string and its length. The table is
- ordered alphabetically on the string in the node pointed
- to by each entry.
-
- This code fragment reads in strings and either finds the
- corresponding node, in which case it prints out the string
- and its length, or it prints an error message.
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define TABSIZE 1000
- struct node /* These are stored in the table */
- {
- char *string;
- int length;
- };
-
- struct node table[TABSIZE]; /* Table to be searched */
- .
- .
- .
- {
- struct node *node_ptr, node;
-
-
-
- MiNT docs 0.1 3 March 1993 1
-
-
-
-
-
- BSEARCH(3) MINTLIB LIBRARY FUNCTIONS BSEARCH(3)
-
-
- int node_compare(const struct *node1, const struct *node2);
- char str_space[20]; /* space to read string into */
- .
- .
- .
- node.string = str_space;
- while (scanf("%s", node.string) != EOF)
- {
- node_ptr = (struct node *)bsearch(&node, table, TABSIZE,
- sizeof(struct node), node_compare);
- if (node_ptr != NULL)
- printf("string = %20s, length = %dn",
- node_ptr->string, node_ptr->length);
- else
- printf("not found: %sn", node.string);
- }
- }
-
- /* Compare two nodes based on an alphabetical */
- /* ordering of the string field. */
- int node_compare(const void *table_node, const void *key_node)
- {
- const struct node *table = (const struct node *)table_node;
- const struct node *key = (const struct node *)key_node;
-
- return(strcmp(table->string, key->string));
- }
-
- S✓SE✓EE✓E A✓AL✓LS✓SO✓O
- q✓qs✓so✓or✓rt✓t(✓(3✓3)✓)
-
- N✓NO✓OT✓TE✓ES✓S
- The comparison function need not compare every byte, so
- arbitrary data may be contained in the elements in addi-
- tion to the values being compared.
-
- A NULL pointer is returned if the key cannot be found in
- the table.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- MiNT docs 0.1 3 March 1993 2
-
-
-